from typing import List


class Solution:
    """
    方法：将矩阵中元素为0的行和列全部置为0
    
    Args:
        matrix: List[List[int]], 二维整数矩阵
        
    Returns:
        None: 直接修改输入矩阵
        
    Time: O(m*n), 其中m是矩阵行数，n是矩阵列数
    Space: O(1), 使用常数空间
    """
    def setZeroes(self, matrix: List[List[int]]) -> None:
        m, n = len(matrix), len(matrix[0])
        flag_colo0 = False
        for i in range(m):
            if matrix[i][0] == 0:
                flag_colo0 = True
            for j in range (1, n):
                if matrix[i][j] == 0:
                    matrix[i][0] = matrix[0][j] = 0
        for i in range(m - 1, -1, -1):
            for j in range(1, n):
                if matrix[i][0] == 0 or matrix[0][j] == 0:
                    matrix[i][j] = 0
            if flag_colo0:
                matrix[i][0] = 0
        